var param = JSON.parse(localStorage.getItem('param'));
var supplyId ;
if(param){
	supplyId = param.supplyId;
}
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	if(supplyId){
		//回显
		$.ajax({
			type : "get",
			url : server_url
					+ 'rest/tea/product/getTeaProductInfo.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				productId:supplyId
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					var product = result.data;
					$('#name').val(product.productname);
					$('#teaPhone').val(product.teaPhone);
					$('#legalIdCard').val(product.legalIdcard);
					$('#detailAddress').val(product.detailAddress);
					$('#teaAge').val(product.teaAge||'');
					$('#teaLocation').val(product.teaLocation||'');
					$('#teaPrice').val(product.teaPrice||'');
					$('#teaNotice').val(product.teaNotice||'');
					
					
					//性别
					var teaSexs = $("input[name='teaSex']");
					teaSexs.removeProp('checked');
					if(product.teaSex){
						$(teaSexs[product.teaSex]).prop("checked",true);
					}
					
					//是否出差
					var teaTrips = $("input[name='teaTrip']");
					teaTrips.removeProp('checked');
					if(product.teaTrip){
						$(teaTrips[product.teaTrip]).prop("checked",true);
					}
					
					//是否提供食宿
					var teaDinners = $("input[name='teaDinner']");
					teaDinners.removeProp('checked');
					if(product.teaDinner){
						$(teaDinners[product.teaDinner]).prop("checked",true);
					}
					
					form.render('radio');
					
					//人员的行业
					var teaIndustrys = $("input[name='teaIndustry']");
					teaIndustrys.removeProp('checked');
					if(product.teaIndustry){
						var ss = product.teaIndustry.split(",");
						$.each(ss,function(i,n){
							$(teaIndustrys[n]).prop("checked",true);
						});
					}
					form.render('checkbox');
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	}
}
//初始化绑定事件
function initButton(){
	$('.address-btn-box').on('click',function(){ 
		var name = $('#name').val();
		var teaPhone = $('#teaPhone').val();
		var legalIdCard = $('#legalIdCard').val();
		var detailAddress = $('#detailAddress').val();
		var teaAge = $('#teaAge').val();
		var teaLocation = $('#teaLocation').val();
		var teaPrice = $('#teaPrice').val();
		var teaNotice = $('#teaNotice').val();
		
		var teaSex = $('input[name="teaSex"]:radio:checked').val();
		var teaTrip = $('input[name="teaTrip"]:radio:checked').val();
		
		var $teaIndustry = $('input:checkbox:checked');
		
		var teaIndustry = '';
		
		$.each($teaIndustry,function(i,n){
			teaIndustry += $(this).attr('value')+",";
		});
		
		if(teaIndustry.length>1){
			teaIndustry = teaIndustry.substring(0,teaIndustry.length-1)
		}
		
		if(!checkNull(name,"请输入联系人姓名")){
			return;
		}
		if(!checkNull(teaPhone,"请输入联系人电话")){
			return;
		}
		if(!checkNull(legalIdCard,"请输入联系人身份证号")){
			return;
		}
		if(!checkNull(detailAddress,"请输入联系人地址")){
			return;
		}
		
		$.ajax({
			type : "post",
			url : server_url
					+ 'rest/tea/supply/demand2.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				name :name,
				teaPhone :teaPhone,
				legalIdCard:legalIdCard,
				detailAddress :detailAddress,
				teaAge :teaAge,
				teaLocation :teaLocation,
				teaPrice :teaPrice,
				teaNotice :teaNotice,
				teaSex:teaSex,
				teaTrip:teaTrip,
				teaIndustry:teaIndustry,
				supplyId:supplyId,
				lat:localStorage.getItem("lat"),
				lon:localStorage.getItem("lon"),
				userId:localStorage.getItem("userid")
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					jumpToIndex("发布成功",2000);
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	});
}